﻿using EONFULLSIN2.Core;
using FreeSql;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EONFULLSIN2.Services.DB
{
    public class FreeSqlHisContext : IFreeSqlHisContext
    {
        public IFreeSql DbContext { get; private set; }

        public FreeSqlHisContext()
        {
            string dbFilePath = AppDomain.CurrentDomain.BaseDirectory + "Common\\DataBase\\";
            string historyFileName = dbFilePath + "EonFullHistory.db";
            if (!Directory.Exists(dbFilePath))
            {
                Directory.CreateDirectory(dbFilePath);
            }
            if (!File.Exists(historyFileName))
            {
                LogManager.Error("create db file");
                File.Create(historyFileName);
            }
            System.Threading.Thread.Sleep(100);
            DbContext = new FreeSqlBuilder()
                            .UseConnectionString(DataType.Sqlite, $"Data Source={historyFileName}")
                            .UseAutoSyncStructure(true)
                            .UseMonitorCommand((cmd) =>
                            {
                                 LogManager.Debug(cmd.CommandText);
                            })
                            .Build();
        }

    }
}
